﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract KontoKolona typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class KontoKolonaDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<KontoKolonaDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.KontoKolona _currentKontoKolona = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxKontoKolonaDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxKontoKolonaErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxKontoKolonaBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDKonto property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDKontoDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDIspisKolona property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDIspisKolonaDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.IspisKolona> _IDIspisKolonaList;
		
		/// <summary> 
		/// The list of selectable IspisKolona
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.IspisKolona> IDIspisKolonaList
		{
			get {return this._IDIspisKolonaList;}
			set 
			{
				this._IDIspisKolonaList = value;
				this.uxIDIspisKolonaDataGridViewColumn.DataSource = null;
				this.uxIDIspisKolonaDataGridViewColumn.DataSource = this._IDIspisKolonaList;
			}
		}
		
		private bool _allowNewItemInIDIspisKolonaList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of IspisKolona
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDIspisKolonaList
		{
			get { return _allowNewItemInIDIspisKolonaList;}
			set
			{
				this._allowNewItemInIDIspisKolonaList = value;
				this.uxIDIspisKolonaDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Konto> _IDKontoList;
		
		/// <summary> 
		/// The list of selectable Konto
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Konto> IDKontoList
		{
			get {return this._IDKontoList;}
			set 
			{
				this._IDKontoList = value;
				this.uxIDKontoDataGridViewColumn.DataSource = null;
				this.uxIDKontoDataGridViewColumn.DataSource = this._IDKontoList;
			}
		}
		
		private bool _allowNewItemInIDKontoList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Konto
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDKontoList
		{
			get { return _allowNewItemInIDKontoList;}
			set
			{
				this._allowNewItemInIDKontoList = value;
				this.uxIDKontoDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.KontoKolona> _KontoKolonaList;
				
		/// <summary> 
		/// The list of KontoKolona to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.KontoKolona> KontoKolonaList
		{
			get {return this._KontoKolonaList;}
			set
			{
				this._KontoKolonaList = value;
				this.uxKontoKolonaBindingSource.DataSource = null;
				this.uxKontoKolonaBindingSource.DataSource = value;
				this.uxKontoKolonaDataGridView.DataSource = null;
				this.uxKontoKolonaDataGridView.DataSource = this.uxKontoKolonaBindingSource;				
				//this.uxKontoKolonaBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxKontoKolonaBindingSource_ListChanged);
				this.uxKontoKolonaBindingSource.CurrentItemChanged += new System.EventHandler(OnKontoKolonaBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnKontoKolonaBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentKontoKolona = uxKontoKolonaBindingSource.Current as Entities.KontoKolona;
			
			if (_currentKontoKolona != null)
			{
				_currentKontoKolona.Validate();
			}
			//_KontoKolona.Validate();
			OnCurrentEntityChanged();
		}

		//void uxKontoKolonaBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.KontoKolona"/> instance.
		/// </summary>
		public Entities.KontoKolona SelectedKontoKolona
		{
			get {return this._currentKontoKolona;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxKontoKolonaDataGridView.VirtualMode;}
			set
			{
				this.uxKontoKolonaDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxKontoKolonaDataGridView.AllowUserToAddRows;}
			set {this.uxKontoKolonaDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxKontoKolonaDataGridView.AllowUserToDeleteRows;}
			set {this.uxKontoKolonaDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxKontoKolonaDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxKontoKolonaDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="KontoKolonaDataGridViewBase"/> class.
		/// </summary>
		public KontoKolonaDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxKontoKolonaDataGridView = new System.Windows.Forms.DataGridView();
			this.uxKontoKolonaBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxKontoKolonaErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDKontoDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDIspisKolonaDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxIDIspisKolonaBindingSource = new IspisKolonaBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDIspisKolonaBindingSource)).BeginInit();
			//this.uxIDKontoBindingSource = new KontoBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKontoBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxKontoKolonaDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxKontoKolonaBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxKontoKolonaErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxKontoKolonaErrorProvider
			// 
			this.uxKontoKolonaErrorProvider.ContainerControl = this;
			this.uxKontoKolonaErrorProvider.DataSource = this.uxKontoKolonaBindingSource;						
			// 
			// uxKontoKolonaDataGridView
			// 
			this.uxKontoKolonaDataGridView.AutoGenerateColumns = false;
			this.uxKontoKolonaDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxKontoKolonaDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDKontoDataGridViewColumn,
		this.uxIDIspisKolonaDataGridViewColumn			});
			this.uxKontoKolonaDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxKontoKolonaDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxKontoKolonaDataGridView.Name = "uxKontoKolonaDataGridView";
			this.uxKontoKolonaDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxKontoKolonaDataGridView.TabIndex = 0;	
			this.uxKontoKolonaDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxKontoKolonaDataGridView.EnableHeadersVisualStyles = false;
			this.uxKontoKolonaDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnKontoKolonaDataGridViewDataError);
			this.uxKontoKolonaDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnKontoKolonaDataGridViewCellValueNeeded);
			this.uxKontoKolonaDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnKontoKolonaDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDKontoDataGridViewColumn
			//
			this.uxIDKontoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDKontoDataGridViewColumn.DataPropertyName = "IDKonto";
			this.uxIDKontoDataGridViewColumn.HeaderText = "IDKonto";
			this.uxIDKontoDataGridViewColumn.Name = "uxIDKontoDataGridViewColumn";
			this.uxIDKontoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDKontoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDKontoDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDIspisKolonaDataGridViewColumn
			//
			this.uxIDIspisKolonaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDIspisKolonaDataGridViewColumn.DataPropertyName = "IDIspisKolona";
			this.uxIDIspisKolonaDataGridViewColumn.HeaderText = "IDIspisKolona";
			this.uxIDIspisKolonaDataGridViewColumn.Name = "uxIDIspisKolonaDataGridViewColumn";
			this.uxIDIspisKolonaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDIspisKolonaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDIspisKolonaDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDIspisKolonaDataGridViewColumn
			//				
			this.uxIDIspisKolonaDataGridViewColumn.DisplayMember = "Naziv";	
			this.uxIDIspisKolonaDataGridViewColumn.ValueMember = "ID";	
			this.uxIDIspisKolonaDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDIspisKolonaDataGridViewColumn.DataSource = uxIDIspisKolonaBindingSource;				
				
			//
			// uxIDKontoDataGridViewColumn
			//				
			this.uxIDKontoDataGridViewColumn.DisplayMember = "Sifra";	
			this.uxIDKontoDataGridViewColumn.ValueMember = "ID";	
			this.uxIDKontoDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDKontoDataGridViewColumn.DataSource = uxIDKontoBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxKontoKolonaDataGridView);
			this.Name = "KontoKolonaDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDIspisKolonaBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKontoBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxKontoKolonaErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxKontoKolonaDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxKontoKolonaBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				KontoKolonaDataGridViewEventArgs args = new KontoKolonaDataGridViewEventArgs();
				args.KontoKolona = _currentKontoKolona;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class KontoKolonaDataGridViewEventArgs : System.EventArgs
		{
			private Entities.KontoKolona	_KontoKolona;
	
			/// <summary>
			/// the  Entities.KontoKolona instance.
			/// </summary>
			public Entities.KontoKolona KontoKolona
			{
				get { return _KontoKolona; }
				set { _KontoKolona = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxKontoKolonaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnKontoKolonaDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxKontoKolonaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnKontoKolonaDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxKontoKolonaDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = KontoKolonaList[e.RowIndex].ID;
						break;
					case "uxIDKontoDataGridViewColumn":
						e.Value = KontoKolonaList[e.RowIndex].IDKonto;
						break;
					case "uxIDIspisKolonaDataGridViewColumn":
						e.Value = KontoKolonaList[e.RowIndex].IDIspisKolona;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxKontoKolonaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnKontoKolonaDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxKontoKolonaDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						KontoKolonaList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDKontoDataGridViewColumn":
						KontoKolonaList[e.RowIndex].IDKonto = (System.Int32)e.Value;
						break;
					case "uxIDIspisKolonaDataGridViewColumn":
						KontoKolonaList[e.RowIndex].IDIspisKolona = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
